package com.right.health.app.ui.me;

import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.DrugReminderInfo;
import com.right.health.app.common.DataCommon;
import com.right.health.app.constants.BroadCastCode;
import com.right.health.app.constants.BroadCastKey;
import com.right.health.app.net.RemindApi;

/*
 * Created by hopeliao on 2019-09-30
 *
 */
public class DrugRemindDetailActivity extends BaseActivity implements View.OnClickListener, OnHttpListener {

    private static final int HTTP_CODE_DEL = 1;
    private static final int HTTP_CODE_UPDATE_REMIND = 2;

    private TextView mNameTv, mStartTimeTv, mEndTimeTv, mCycleTv, mCycleTimeTv;

    private DrugReminderInfo mResponse;

    private RemindApi mRemindApi;
    private SwitchCompat mRemindCompat;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.drug_reminder_detail);
    }

    @Override
    public void findView() {
        mNameTv = (TextView) findViewById(R.id.tv_name);
        mStartTimeTv = (TextView) findViewById(R.id.tv_startTime);
        mEndTimeTv = (TextView) findViewById(R.id.tv_endTime);
        mCycleTv = (TextView) findViewById(R.id.tv_cycle);
        mCycleTimeTv = (TextView) findViewById(R.id.tv_cycle_time);
        findViewById(R.id.tv_del).setOnClickListener(this);
        mRemindCompat = (SwitchCompat) findViewById(R.id.switchcompat_remind);

    }

    @Override
    public void initialize() {
        mRemindApi = new RemindApi();
        mRemindApi.setListener(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(BroadCastKey.DATA)) {
            mResponse = bundle.getParcelable(BroadCastKey.DATA);

            refreshUI();
        }
    }

    private void refreshUI() {
        if (mResponse != null) {
            mNameTv.setText(mResponse.remindname);
            mStartTimeTv.setText(mResponse.getStartTime());
            mEndTimeTv.setText(mResponse.getEndTime());
            mCycleTimeTv.setText(mResponse.time);
            mCycleTv.setText(mResponse.times);
            mCycleTv.setText(String.format("1天%s次", mResponse.times));

            boolean checked = false;
            if ("1".equals(mResponse.remindstatus)) {
                checked = true;
            }
            mRemindCompat.setChecked(checked);
            mRemindCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(mResponse != null) {
                        String target = isChecked ? "1" : "0";

                        mRemindApi.setTag(target);
                        mRemindApi.addRequestCode(HTTP_CODE_UPDATE_REMIND);
                        mRemindApi.modifyRemind(mResponse.remindid, target);
                    }
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_del) {
            final AlertDialog dialog = DataCommon.getDeleteDialog(this, (dialog1, which) -> {
                dialog1.dismiss();
                if (mResponse != null) {
                    mRemindApi.addRequestCode(HTTP_CODE_DEL);
                    mRemindApi.delete(mResponse.remindid);
                }
            });
            dialog.show();
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_DEL) {
                ToastUtil.show(this, "删除成功");
                sendBroadcast(BroadCastCode.RREFRESH_DRUGREINDE);
                finish();
            } else if(request.requestCode == HTTP_CODE_UPDATE_REMIND) {
                String target = String.valueOf(request.tag);
                if(mResponse != null) {
                    mResponse.remindstatus = target;
                }
                ToastUtil.show(this, "修改成功");
                sendBroadcast(BroadCastCode.RREFRESH_DRUGREINDE);
            }

        }
    }
}
